package com.walterweimod.auxiliary_attributes.event;

import com.walterweimod.auxiliary_attributes.attribute.ModAttributes;
import com.walterweimod.auxiliary_attributes.mobeffect.ModMobEffects;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.AttributeInstance;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraftforge.event.entity.living.LivingDamageEvent;
import net.minecraftforge.event.entity.living.LivingHealEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import java.util.Objects;

import static com.walterweimod.auxiliary_attributes.util.ModAttributeUtils.*;
import static com.walterweimod.auxiliary_attributes.Auxiliary_attributes.*;
import static com.walterweimod.auxiliary_attributes.util.ModUtils.*;
import static com.walterweimod.auxiliary_attributes.mobeffect.Resilience.*;

@Mod.EventBusSubscriber
public class ResilienceEvent {
    @SubscribeEvent
    public static void onLivingEntityBeingDamage(LivingDamageEvent event){
        LivingEntity livingEntity = event.getEntityLiving();
        MobEffectInstance resilience = livingEntity.getEffect(ModMobEffects.RESILIENCE.get());

        if (Objects.nonNull(resilience)){
            modifyAttributes(livingEntity, resilience.getAmplifier());
        }
    }

    @SubscribeEvent
    public static void onLivingHeal(LivingHealEvent event){
        LivingEntity livingEntity = event.getEntityLiving();
        MobEffectInstance resilience = livingEntity.getEffect(ModMobEffects.RESILIENCE.get());

        if (Objects.nonNull(resilience)){
            modifyAttributes(livingEntity, resilience.getAmplifier());
        }
    }

    private static void modifyAttributes(LivingEntity livingEntity, int amplifier){
        if (Objects.nonNull(livingEntity)){
            AttributeInstance defense = livingEntity.getAttribute(ModAttributes.DEFENSE.get());
            AttributeInstance attack = livingEntity.getAttribute(Attributes.ATTACK_DAMAGE);
            AttributeInstance special_defense = livingEntity.getAttribute(ModAttributes.SPECIAL_DEFENSE.get());
            float reducePercent = 100.0F - getLivingEntityHealthPercent(livingEntity);

            if (Objects.nonNull(defense)){
                removeAttributeModifier(
                        livingEntity,
                        defense.getAttribute(),
                        getResilienceDefenseUUID()
                );

                addAttributeModifier(
                        livingEntity,
                        defense.getAttribute(),
                        (0.008D + 0.005 * amplifier) * reducePercent,
                        getResilienceDefenseUUID(),
                        AttributeModifier.Operation.MULTIPLY_BASE,
                        false
                );

                getLogger().info(hyphenName("Resilience Defense", 15));
                getLogger().info("毁灭性吸积总防御力：{}", defense.getValue());
            }

            if (Objects.nonNull(attack)){
                removeAttributeModifier(
                        livingEntity,
                        attack.getAttribute(),
                        getResilienceAttackUUID()
                );

                addAttributeModifier(
                        livingEntity,
                        attack.getAttribute(),
                        (0.015D + 0.008 * amplifier) * reducePercent,
                        getResilienceAttackUUID(),
                        AttributeModifier.Operation.MULTIPLY_BASE,
                        false
                );

                getLogger().info(hyphenName("Resilience Attack", 15));
                getLogger().info("毁灭性吸积总攻击力：{}", attack.getValue());
            }

            if (Objects.nonNull(special_defense)){
                removeAttributeModifier(
                        livingEntity,
                        special_defense.getAttribute(),
                        getResilienceSpecialDefenseUUID()
                );

                addAttributeModifier(
                        livingEntity,
                        special_defense.getAttribute(),
                        (0.005D + 0.003 * amplifier) * reducePercent,
                        getResilienceSpecialDefenseUUID(),
                        AttributeModifier.Operation.MULTIPLY_BASE,
                        false
                );

                getLogger().info(hyphenName("Resilience Special Defense", 15));
                getLogger().info("毁灭性吸积总特殊防御力：{}", special_defense.getValue());
            }
        }
    }
}
